import java.util.concurrent.locks.ReentrantLock;

public class Thread_UnsafeBank {
    public static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Account account = new Account(100, "12138");
        Drawing u1 = new Drawing(account, 60, "小明");
        Drawing u2 = new Drawing(account, 60, "小美");
        u1.start();
        u2.start();
    }


    //账户
    static class Account {

        int money;
        String id;

        public Account(int money, String id) {
            this.money = money;
            this.id = id;
        }
    }

    //银行
    static class Drawing extends Thread {
        Account account;
        int drawingMoney;//要取多少钱
        int nowMoney;//成功取出来的钱

        public Drawing(Account account, int drawingMoney, String name) {
            super(name);
            this.account = account;
            this.drawingMoney = drawingMoney;
        }

        @Override
        public void run() {
            //synchronized (account) {
            lock.lock();
                //判断钱够不够
                if (account.money - drawingMoney < 0) {
                    System.out.println(Thread.currentThread().getName() + "钱不够");
                    return;
                }

                //模拟延时
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //取钱
                account.money -= drawingMoney;
                //手里的钱
                nowMoney += drawingMoney;
                System.out.println(account.id + "余额为：" + account.money);
                System.out.println(this.getName() + "手里的钱" + nowMoney);
            lock.unlock();
            //}
        }
    }


}